package team.se.tms.controller.project;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.annotation.CheckFormat;
import team.se.tms.annotation.Regex;
import team.se.tms.annotation.SpecialCheck;
import team.se.tms.constant.SessionConstant;
import team.se.tms.controller.BaseController;
import team.se.tms.dto.GroupDetail;
import team.se.tms.dto.Page;
import team.se.tms.dto.ProjectBasicDetail;
import team.se.tms.dto.ProjectDetail;
import team.se.tms.exception.InsertException;
import team.se.tms.exception.UpdateException;
import team.se.tms.po.ProjectInfo;
import team.se.tms.po.TeachCourseInfo;
import team.se.tms.po.TeacherInfo;
import team.se.tms.po.UserInfo;
import team.se.tms.service.ProjectInfoService;
import team.se.tms.service.TeachCourseInfoService;
import team.se.tms.service.TeacherInfoService;
import team.se.tms.util.Jurisdiction;
import team.se.tms.util.ResultUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目信息控制层
 *
 * @author winkyle
 * @date 2018/12/7
 */
@Slf4j
@Controller
@RequestMapping(value = "/project")
public class ProjectInfoController extends BaseController {
    @Resource(name = "ProjectInfoServiceImpl")
    private ProjectInfoService projectInfoService;

    @Resource(name = "TeacherInfoServiceImpl")
    private TeacherInfoService teacherInfoService;

    @Resource(name = "TeachCourseInfoServiceImpl")
    private TeachCourseInfoService teachCourseInfoService;

    /**
     * 根据授课id获取分组信息
     *
     * @param teachCourseId 授课id
     * @param page 分页
     * @return 分组信息
     * @throws Exception 异常
     * @create on 2018/12/7 by winkyle
     */
    @RequestMapping(value = "/search_group_details")
    @ResponseBody
    public Object searchGroupDetails(@SpecialCheck("Pk")String teachCourseId,Page page) throws Exception {
        log.debug("============searchGroupDetails==================={}",teachCourseId);
        List<GroupDetail> groupDetails = projectInfoService.listGroupDetails(teachCourseId, page);
        log.debug("================================List<GroupDetail>{}",groupDetails.toString());
        return page.getDataTabelJson(groupDetails);
    }
    /**
     * 部署项目
     *
     * @param projectId 项目Id
     * @param projectAddress 项目地址
     * @return Object 部署结果
     * @throws Exception 异常
     * @create on 2018/12/28 by sunddly
     */
    @RequestMapping(value = "/editAddress")
    @ResponseBody
    public Object editProjectAddress(String projectId,String projectAddress) throws Exception {
//        projectId="20181208232425OL";
//        projectAddress="http://www.1baidu.com";
        boolean result = projectInfoService.modifyProjectAddress(projectId,projectAddress);
        if(!result){
            throw new UpdateException("部署项目失败，请重新部署");
        }
        return  ResultUtil.success("项目部署成功");
    }


    @RequestMapping("list")
    public String list() throws Exception{
        return "project/project_list";
    }

    /**
     * 查询项目列表
     *
     * @param page
     * @return
     * @throws Exception
     * @create on 2018/12/27 by qingx
     */
    @RequestMapping("getList")
    @ResponseBody
    public String searchAllProjectDetails(Page page,HttpSession httpSession) throws Exception{
        System.out.println("page.getOrderDir()"+page.getOrderDir());
        List<ProjectDetail> projectDetails =  projectInfoService.listAllProjectDetails(page);
        httpSession.setAttribute("projectDetails",projectDetails);
        System.out.println("55555555555555555555555projectDetails"+projectDetails);
        System.out.println("page.getDataTabelJson"+page.getDataTabelJson(projectDetails));
        return page.getDataTabelJson(projectDetails);
    }

    /**
     * 跳转到添加项目页面
     *
     * @return
     * @throws Exception
     * @create on 2018/12/27 by qingx
     */
    @RequestMapping("toAddProject")
    public ModelAndView searchAddProjectDetail() throws Exception{
        ModelAndView mv = this.getModelAndView();
        List<TeachCourseInfo> teachCourseInfos = teachCourseInfoService.listAllTeachCourseInfos(new Page());
        List<TeacherInfo> teacherInfos = teacherInfoService.listAllTeacherInfos(new Page());
        System.out.println("777777777777777teachCourseInfos"+teachCourseInfos);
        System.out.println("777777777777777teacherInfos"+teacherInfos);
        mv.addObject("teachCourseInfos",teachCourseInfos);
        mv.addObject("teacherInfos",teacherInfos);
        mv.setViewName("project/add_project");
        return mv;
    }

    /**
     * 添加项目信息
     *
     * @return
     * @throws Exception
     * @create on 2018/12/28 by qingx
     */
    @RequestMapping(value = "addProjectInfo",method = RequestMethod.POST)
    @ResponseBody
    public Object addProjectInfo(ProjectInfo projectInfo, @RequestParam(value="stuIds[]")String[] stuIds) throws Exception{
        System.out.println("111111111projectInfo"+projectInfo+" "+stuIds);
        boolean result = projectInfoService.savaProjectInfo(projectInfo,stuIds);
        if(result == true){
            return  ResultUtil.success("项目录入成功");
        }
        throw new InsertException("项目录入失败");
    }

    /**
     * 跳转到修改项目界面
     *
     * @return ModelAndView
     * @throws Exception 异常
     * @create on 2018/12/29 by qingx
     */
    @RequestMapping("toEditProject")
    public ModelAndView viewEditProject(@Regex("^[A-Za-z0-9]{16}$") String projectId, HttpSession session) throws Exception{
        Gson gson = new Gson();
        ModelAndView mv = this.getModelAndView();
        //从session中获取labelLibDetails，取到id 等于要修改的标签库的id
        List<ProjectDetail> projectDetails = (List<ProjectDetail>)session.getAttribute("projectDetails");
        ProjectDetail projectDetail = new ProjectDetail();
        if(projectDetails!=null && projectDetails.size()>0){
            for(ProjectDetail item:projectDetails){
                if(item.getProjectId().equals(projectId)){
                    projectDetail = item;
                    break;
                }
            }
        }
        mv.addObject("projectDetail",projectDetail);
        List<TeachCourseInfo> teachCourseInfos = teachCourseInfoService.listAllTeachCourseInfos(new Page());
        List<TeacherInfo> teacherInfos = teacherInfoService.listAllTeacherInfos(new Page());
        System.out.println("777777777777777teachCourseInfos"+teachCourseInfos);
        System.out.println("777777777777777teacherInfos"+teacherInfos);
        System.out.println("777777777777777projectDetail"+projectDetail);
        mv.addObject("teachCourseInfos",teachCourseInfos);
        mv.addObject("teacherInfos",teacherInfos);
        mv.setViewName("project/modify_project");
        return mv;
    }

    /**
     * 修改项目信息
     *
     * @param stuIds 授课信息
     * @return
     * @throws Exception
     * @create on 2018/12/18 by qingx
     */
    @RequestMapping(value = "editProject",method = RequestMethod.POST)
    @CheckFormat({"projectDeploymentAddress"})
    @ResponseBody
    public Object editProject(ProjectInfo projectInfo,@RequestParam(value="stuIds[]")String[] stuIds) throws Exception{
        System.out.println("111111111projectInfo"+projectInfo);
        boolean result = projectInfoService.modifyProject(projectInfo,stuIds);
        if(result == true){
            return  ResultUtil.success("项目修改成功");
        }else{
            throw new UpdateException("项目修改失败");
        }
    }


    /**
     * 根据用户id和授课id查看我的项目信息
     *
     * @param teachCourseId 授课id
     * @return ModelAndView
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @RequestMapping("searchMyProjects")
    public ModelAndView searchMyProjects(String teachCourseId) throws Exception{
        String userId = ((UserInfo) Jurisdiction.getSession().getAttribute(SessionConstant.USER)).getUserId();
        teachCourseId = "20181208232425OL";
        ModelAndView mv = new ModelAndView();
        List<ProjectInfo> projectInfoList = projectInfoService.listProjectInfo(teachCourseId,userId);
        mv.addObject("projectInfoList",projectInfoList);
        mv.setViewName("project/my_project");
        return mv;
    }
    /**
     * 根据项目id获取项目基本详情
     *
     * @param projectId 项目Id
     * @return ModelAndView
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @RequestMapping("searchMyProjectDetail")
    public ModelAndView searchMyProjectBasicDetail(String projectId,HttpSession session) throws Exception{
        System.out.println("((((((((((((((((((("+projectId);
        ModelAndView mv =this.getModelAndView();
        ProjectBasicDetail projectBasicDetail = projectInfoService.getProjectBasicDetail(projectId);
        session.setAttribute("projectBasicDetail",projectBasicDetail);
        //System.out.println("****((((((((((((((((((("+projectBasicDetail.getAssignmentFiles());
        mv.addObject("projectBasicDetail",projectBasicDetail);
        mv.setViewName("project/foot_my_project");
        return mv;
    }
}
